﻿using Networking;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chat
{
    public partial class frmServer : Form
    {
        private ServerManager Server;
        private int ClientCount;
        
        public frmServer()
        {
            InitializeComponent();
            ClientCount = 0;
            TextBox.CheckForIllegalCrossThreadCalls = false;
            Button.CheckForIllegalCrossThreadCalls = false;
            CheckBox.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (txtPort.Text.Length > 0)
            {
                int port = int.Parse(txtPort.Text);

                if (port > 20)
                {
                    Server = new ServerManager(port);
                    Server.OnServerInit += Server_OnServerInit;
                    Server.OnClientConnect += Server_OnClientConnect;
                    Server.OnClientDisconnect += Server_OnClientDisconnect;
                    Server.OnReceiveClientData += Server_OnReceiveClientData;
                    Server.Start();
                    btnStart.Enabled = false;
                }
                else
                {
                    MessageBox.Show("ERROR: Make sure the port is greater than 20.");
                }
            }
            else
            {
                MessageBox.Show("ERROR: Put a Port to start server.");
            }
        }

        private void Server_OnReceiveClientData(int clientid, Headers header, SubHeaders subheader, string data)
        {
            Log("SERVER: Received data from Client " + clientid.ToString() + ".");
            Log("SERVER: Data is '" + data + "'.");
            Log("SERVER: Header is '" + header.ToString() + "'.");
            Log("SERVER: Subheader is '" + subheader.ToString() + "'.");
        }

        private void Server_OnClientDisconnect(int clientid)
        {
            ClientCount--;
            if (ClientCount > 1) btnSend.Enabled = true;
            else btnSend.Enabled = false;
            Log("SERVER: Client " + clientid.ToString() + " disconnected.");
        }

        private void Server_OnClientConnect(int clientid, string ip)
        {
            ClientCount++;
            if (ClientCount > 1) btnSend.Enabled = true;
            else btnSend.Enabled = false;
            Log("SERVER: Client " + clientid.ToString() + " connected with IP " + ip + ".");
        }

        private void Server_OnServerInit(int port)
        {
            Log("SERVER: Initialized on port " + port.ToString() + ".");
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            int clientid = int.Parse(txtClientID.Text);
            string message = txtMessage.Text;

            if (message.Length > 0)
            {
                if (clientid > 0)
                {
                    Server.SendDataToClient(clientid, SubHeaders.Text, message);
                }
                else
                {
                    Server.SendDataToAll(SubHeaders.Text, message);
                }
            }
            else
            {
                MessageBox.Show("Enter a message first.");
            }
        }

        private void Log(string text)
        {
            txtLog.Invoke((MethodInvoker)delegate 
            {
                txtLog.AppendText(text + "\r\n");
            });
        }

        private void chckAllClients_CheckedChanged(object sender, EventArgs e)
        {
            txtClientID.Enabled = !chckAllClients.Checked;
        }
    }
}
